#include "UtilityPCH.h"

#include "UT_SqliteHandler.h"
#include "UT_SqliteResult.h"
#include "UT_StringHelper.h"

using namespace Utility;
//-----------------------------------------------------------------------------
//UT_SQLite_Handler
//-----------------------------------------------------------------------------
UT_SQLite_Handler::UT_SQLite_Handler()
:m_pkHandler(NULL)
,m_bOpened(false)
{
	m_szFileName[0] = '\0';
}
//-----------------------------------------------------------------------------
UT_SQLite_Handler::~UT_SQLite_Handler ()
{
	Close();
}
//-----------------------------------------------------------------------------
UT_SQLite_Handler::UT_SQLite_Handler(const char* szFileName)
{
	STRNCPY(m_szFileName, szFileName, sizeof(m_szFileName));

	m_bOpened = false;
	
	_open ();
}
//-----------------------------------------------------------------------------
int32_t UT_SQLite_Handler::Open(const char* szFileName)
{
	if (m_bOpened)
	{
		Close();
	}

	STRNCPY(m_szFileName, szFileName, sizeof(m_szFileName));

	return _open ();
}
//-----------------------------------------------------------------------------
int32_t UT_SQLite_Handler::Close (void)
{
	if (NULL != m_pkHandler)
	{
		sqlite3_close (m_pkHandler);
	
		m_pkHandler = NULL;
		m_bOpened = false;
	}

	return 0;
}
//-----------------------------------------------------------------------------
int32_t UT_SQLite_Handler::Execute(const char* szSql)
{
	if (NULL == szSql)
	{
		return -1;
	}

	if (!m_bOpened)
	{
		if (0 != _open () )
		{
			return -1;
		}
	}

	char* error = NULL;
	int ret = sqlite3_exec (m_pkHandler, szSql, NULL, NULL, &error);
	
	if (SQLITE_OK == ret)
	{
		return 0;
	}

	if (NULL != error)
	{
		sqlite3_free (error);
	}

	return -1;
}
//-----------------------------------------------------------------------------
int32_t UT_SQLite_Handler::Execute(const char* szSql, UT_SQLite_Result& rkResult)
{
	if (NULL == szSql)
	{
		return -1;
	}

	if (!m_bOpened)
	{
		if (0 != _open())
		{
			return -1;
		}
	}

	char** result = NULL;
	int row = 0;
	int col = 0;
	char* error = NULL;
	int ret = sqlite3_get_table (m_pkHandler, szSql, &result, &row, &col, &error);
	if (SQLITE_OK == ret && NULL != result && 0 <= row && 0 <= col)
	{
		rkResult.SetResultData(result, (size_t)row, (size_t)col);
		return 0;
	}

	if (NULL != error)
	{
		sqlite3_free (error);
	}

	return -1;
}
//-----------------------------------------------------------------------------
int UT_SQLite_Handler::GetLastError()
{
	return sqlite3_errcode (m_pkHandler);
}
//-----------------------------------------------------------------------------
int32_t UT_SQLite_Handler::_open (void)
{
	if (m_bOpened)
	{
		Close ();
	}

	int ret = sqlite3_open (m_szFileName, &m_pkHandler);
	
	if (SQLITE_OK == ret)
	{
		m_bOpened = true;
		return 0;
	}
	else
	{
		m_bOpened = false;
	}

	return -1;
}





